package com.testingedu.jdbcDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.testingedu.common.QueryResult;
import com.testingedu.db.DBManager;
import com.testingedu.model.TIndexPicture;

public class TIndexPictureDao extends BaseDao<TIndexPicture>{
	public void add(TIndexPicture tIndexPicture) throws Exception{
		String sql = "insert into t_pic (path, is_show) value(?,?)";
		List<Object> values = new ArrayList<Object>();
		values.add(tIndexPicture.getPath());
		if(tIndexPicture.getIsShow() == null || tIndexPicture.getIsShow().length() == 0)
			tIndexPicture.setIsShow("n");
		else {
			tIndexPicture.setIsShow("y");
		}
		values.add(tIndexPicture.getIsShow());
		try {
			this.myJdbcSetValuesAndExecuteSaveOrUpdateOrDelete(sql, values);
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception();
		} finally {
			this.close();
		}
	}
	
	public void update(TIndexPicture tIndexPicture) throws Exception {
		String sql = "update t_pic tp set tp.is_show = ? where tp.id = ?";
		List<Object> values = new ArrayList<Object>();
		if("n".equals(tIndexPicture.getIsShow()))
			tIndexPicture.setIsShow("y");
		else {
			tIndexPicture.setIsShow("n");
		}
		values.add(tIndexPicture.getIsShow());
		values.add(tIndexPicture.getId());
		try {
			this.myJdbcSetValuesAndExecuteSaveOrUpdateOrDelete(sql, values);
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception();
		} finally {
			this.close();
		}
	}
	
	public static List<TIndexPicture> index(int count) {
		String sql = "select * from t_pic tp where tp.is_show='y' order by id desc limit "
				+ count + "";
		Connection conn = DBManager.getConnection();
		List<TIndexPicture> result = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			result = _resultSetToList(rs);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (rs != null)
					rs.close();
				if (ps != null)
					ps.close();
				if (conn != null)
					conn.close();
			} catch (SQLException e) {
			}
		}
		return result;
	}
	
	public void list(QueryResult queryResult, TIndexPicture tIndexPicture) throws Exception{
		StringBuffer sql = new StringBuffer("select * from t_pic tp where 1=1");
		List<Object> values = new ArrayList<Object>();
		sql.append(" limit ?,?");
		List<Object> countValues = new ArrayList<Object>(values);
		values.add(queryResult.getStartRow());
		values.add(queryResult.getPageSize());
		try {
			this.myJdbcSetValuesAndExecuteQuery(sql.toString(), values);
			queryResult.setResultList(_resultSetToList(rs));
		}
		finally {
			close();
		}
		sql.replace(7, 8, "count(*)");
		try {
			this.myJdbcSetValuesAndExecuteQuery(sql.substring(0, sql.length() - 10), countValues);
			rs.next();
			queryResult.setAllCount(new Long(rs.getInt(1)));
		} catch(SQLException e) {
			e.printStackTrace();
			throw new Exception();
		}
		finally {
			close();
		}
	}
	
	public TIndexPicture info(int id) throws Exception{
		String sql = "select * from t_pic tp where tp.id = ?";
		List<TIndexPicture> result = null;
		try {
			this.myJdbcSetValuesAndExecuteLoad(sql, id);
			result = _resultSetToList(rs);
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception();
		}
		finally {
			close();
		}
		if(result != null && result.size() > 0) {
			return result.get(0);
		}
		return null;
	}
	

	public void delete(int id) throws Exception {
		String sql = "delete from t_pic where id = ?";
		try {
			this.myJdbcSetValuesAndExecuteDelete(sql, id);
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception();
		} finally {
			close();
		}
	}
	public static List<TIndexPicture> _resultSetToList(ResultSet rs) throws Exception{
		List<TIndexPicture> result = new ArrayList<TIndexPicture>();
		try {
			while(rs.next()) {
				TIndexPicture tIndexPicture = new TIndexPicture();
				tIndexPicture.setId(rs.getInt("id"));
				tIndexPicture.setPath(rs.getString("path"));
				tIndexPicture.setName(rs.getString("name"));
				tIndexPicture.setIsShow(rs.getString("is_show"));
				result.add(tIndexPicture);
			}
		} catch (SQLException e) {
			e.printStackTrace();
			throw new Exception();
		}
		return result;
	}
}
